package com.comleader.uap.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.common.core.web.page.PageResult;
import com.comleader.uap.common.page.PageQuery;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.UrlVisitMapper;
import com.comleader.uap.pojo.entity.AppAnalyse;
import com.comleader.uap.pojo.entity.UrlVisit;
import com.comleader.uap.pojo.query.AppAnalyseQuery;
import com.comleader.uap.pojo.query.UrlVisitQuery;
import com.comleader.uap.pojo.vo.UrlVisitDetailVO;
import com.comleader.uap.pojo.vo.UrlVisitVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import static cn.hutool.core.date.DateUtil.formatDate;

/**
 * @author 王坤
 * @description 针对表【lingyun_url_pegging(非法网站访问表结构)】的数据库操作Service
 * @createDate 2024-11-26 18:10:00
 */
@DS("mysql-ly")
@Service
public class UrlVisitService extends ServiceImpl<UrlVisitMapper, UrlVisit> {


    public PageResult<UrlVisitVO> selectList(PageQuery<UrlVisitQuery> pageQuery) {
        UrlVisitQuery urlVisitQuery = pageQuery.getQueryParams();
        if (CollUtil.isNotEmpty(urlVisitQuery.getDiscoveryTime())) {
            urlVisitQuery.setDiscoveryStartTimestamp(urlVisitQuery.getDiscoveryTime().get(0));
            urlVisitQuery.setDiscoveryEndTimestamp(urlVisitQuery.getDiscoveryTime().get(1));
        }
        PageHelper.startPage(pageQuery.getPageNumber(), pageQuery.getPageSize());
        List<UrlVisit> list = baseMapper.selectList(pageQuery.getQueryParams());
        PageInfo pageInfo = new PageInfo<>(list);
        PageResult<UrlVisitVO> pageResult = new PageResult();
        pageResult.setPageNumber(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        List<UrlVisitVO> voList = new ArrayList<>();
        list.forEach(data -> {
            UrlVisitVO urlVisitVO = new UrlVisitVO();
            urlVisitVO.setId(data.getId());
            urlVisitVO.setUrl(data.getUrl());
            urlVisitVO.setIsBlack(data.getIsBlack());
            urlVisitVO.setIsAbnormal(data.getIsAbnormal());
            urlVisitVO.setVisitCount(data.getVisitCount());
            urlVisitVO.setDiscoveryTime(data.getDiscoveryTime());
            voList.add(urlVisitVO);
        });
        pageResult.setRecords(voList);
        return pageResult;
    }

    public void export(UrlVisitQuery query, HttpServletResponse response) throws IOException {
        List<Date> discoveryTimeList = query.getDiscoveryTime();
        if (CollUtil.isEmpty(query.getDiscoveryTime()) || query.getDiscoveryTime().size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            query.setDiscoveryStartTimestamp(query.getDiscoveryTime().get(0));
            query.setDiscoveryEndTimestamp(query.getDiscoveryTime().get(1));
            String fileName = "非法网站_" + formatDate(discoveryTimeList.get(0)) + "_" + formatDate(discoveryTimeList.get(1)) + ".xlsx";
            List<UrlVisit> list = baseMapper.selectList(query);
            List<UrlVisitVO> voList = new ArrayList<>();
            list.forEach(data -> {
                UrlVisitVO urlVisitVO = new UrlVisitVO();
                urlVisitVO.setId(data.getId());
                urlVisitVO.setUrl(data.getUrl());
                urlVisitVO.setIsBlack("0".equals(data.getIsBlack()) ? "否" : "是");
                urlVisitVO.setIsAbnormal("0".equals(data.getIsAbnormal()) ? "否" : "是");
                urlVisitVO.setVisitCount(data.getVisitCount());
                urlVisitVO.setDiscoveryTime(data.getDiscoveryTime());
                voList.add(urlVisitVO);
            });
            ExcelUtil.exportMultipleSheet(response, fileName, UrlVisitVO.class, "非法网站", voList);
        } catch (Exception e) {
            log.error("非法网站导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }
    }

    public PageResult<UrlVisitDetailVO> selectDetailList(PageQuery<UrlVisitQuery> pageQuery) {
        UrlVisitQuery urlVisitQuery = pageQuery.getQueryParams();
        if (CollUtil.isNotEmpty(urlVisitQuery.getDiscoveryTime())) {
            urlVisitQuery.setDiscoveryStartTimestamp(urlVisitQuery.getDiscoveryTime().get(0));
            urlVisitQuery.setDiscoveryEndTimestamp(urlVisitQuery.getDiscoveryTime().get(1));
        }
        PageHelper.startPage(pageQuery.getPageNumber(), pageQuery.getPageSize());
        List<UrlVisit> list = baseMapper.selectDetailList(pageQuery.getQueryParams());
        PageInfo pageInfo = new PageInfo<>(list);
        PageResult<UrlVisitDetailVO> pageResult = new PageResult();
        pageResult.setPageNumber(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        List<UrlVisitDetailVO> voList = new ArrayList<>();
        list.forEach(data -> {
            UrlVisitDetailVO urlVisitDetailVO = new UrlVisitDetailVO();
            urlVisitDetailVO.setId(data.getId());
            urlVisitDetailVO.setMsisdn(data.getMsisdn());
            urlVisitDetailVO.setVisitCount(data.getVisitCount());
            urlVisitDetailVO.setVisitTime(data.getVisitTime());
            voList.add(urlVisitDetailVO);
        });
        pageResult.setRecords(voList);
        return pageResult;
    }

    public List<Integer> detailFeature(UrlVisitQuery query) {
        Map<String, Integer> timeMap = new LinkedHashMap<>();
        for(int i = 0; i < 24; i++){
            String key = String.format("%02d",i);
            timeMap.put(key,0);
        }
        List<UrlVisit> list = baseMapper.detailFeature(query);
        list.forEach(data -> {
            timeMap.put(data.getHour(), Integer.valueOf(data.getVisitCount()));
        });
        List<Integer> orderedValues = new ArrayList<>(timeMap.values());
        return orderedValues;
    }
}
